'''
	The OutputInstrumentRecording records everything that the instrument does to
	the HeaDSET format. 

	@author: J.H. Donker
	@copyright: Twente University
'''
from wx.lib.pubsub import Publisher
from OutputController import OutputController
from model import *

class OutputInstrumentRecording(OutputController):
	'''
		Records all instrument activity by listening to messages coming from the 
		model. This class inherits from OutputController.
	'''
	def __init__(self):
		'''
			Constructor.
		'''
		OutputController.__init__(self)
		print "OutputInstrumentRecording active"
		self.__recording = None
		self.__running = False
	
	
	def InstrumentStartRecording(self, instrument):
		'''
			Starts recording. Will record first the initial position.
			
			@param instrument: The instrument that will be recorded.
		'''
		print "Start Record"
		self.__running = True
		self.__recording = Recording()
		o = instrument.GetOrientation()
		s = instrument.GetScale()
		p = instrument.GetPosition()

		
		#make a snapshot of the intial state
		atom1 = RecordingAtom(o, RecordingAtom.ORIENTATION)
		atom2 = RecordingAtom(s, RecordingAtom.SCALE)
		atom0 = RecordingAtom(p, RecordingAtom.POSITION)
		
		self.__recording.AddAtom(atom0)
		self.__recording.AddAtom(atom1)
		self.__recording.AddAtom(atom2)
		
	#
	def InstrumentStopRecording(self, instrument):
		'''
			Stops recording. Will record the last position.
			
			@param instrument: The instrument that will be recorded.
		'''
		o = instrument.GetOrientation()
		s = instrument.GetScale()
		p = instrument.GetPosition()
				
		atom1 = RecordingAtom(o, RecordingAtom.ORIENTATION)
		atom2 = RecordingAtom(s, RecordingAtom.SCALE)
		atom0 = RecordingAtom(p, RecordingAtom.POSITION)
		
		self.__recording.AddAtom(atom0)
		self.__recording.AddAtom(atom1)
		self.__recording.AddAtom(atom2)		
		self.__running = False
	
	def GetRecording(self):
		'''
			Gives the recording that was made with this OutputController
			
			@return: The recording if the OutputController is active. None if
			         active. 
		'''
		if not self.__running:
			return self.__recording
		else:
			return None 
	
	def InstrumentPositionUpdated(self, instrument):
		'''
			Records the position update.
			
			@param instrument: The instrument that will be recorded.
		'''
		print "pos update"
		if self.__recording != None:
			atom = RecordingAtom(instrument.GetPosition(), RecordingAtom.POSITION)
			self.__recording.AddAtom(atom)
		
	
	#
	def InstrumentScaleUpdated(self, instrument):
		'''
			Records the scale update.
			
			@param instrument: The instrument that will be recorded.
		'''
		print "scale update"
		if self.__recording != None:
			atom = RecordingAtom(instrument.GetScale(),  RecordingAtom.SCALE)
			self.__recording.AddAtom(atom)
	
	#
	def InstrumentOrientationUpdated(self, instrument):
		'''
			Records the orientation update.
			
			@param instrument: The instrument that will be recorded. 
		'''
		if self.__recording != None:
			atom = RecordingAtom(instrument.GetOrientation(),  RecordingAtom.ORIENTATION)
			self.__recording.AddAtom(atom)
	
